package com.jy.adscm.operation.pebase.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.pebase.bean.PeLabel;
import com.jy.adscm.operation.pebase.service.PeLabelService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-04-18
 */
@RestController
@RequestMapping("/adscm/pebase/peLabel")
public class PeLabelController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    PeLabelService peLabelService;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody PeLabel peLabel, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        boolean insert = peLabelService.insert(peLabel);
        return new SimpleCoreResponse("200","新增成功",insert);
    }

    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id , HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        boolean result = peLabelService.deletepeLabel(id);
        return new  SimpleCoreResponse("200","删除成功",result);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody PeLabel peLabel, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result = peLabelService.updateById(peLabel);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("select")
    public SimpleCoreResponse select( HttpServletRequest request) throws AdscmSystemException{
        List<PeLabel> peLabels = peLabelService.selectList(null);
        return  new SimpleCoreResponse("200","查询成功",peLabels);
    }

    @RequestMapping("selectByType/{type}")
    public SimpleCoreResponse selectUsers(@PathVariable String type,  HttpServletRequest request) throws AdscmSystemException{
        Wrapper<PeLabel> peLabelWrapper=new EntityWrapper<>();
        peLabelWrapper.eq("type",type );
        List<PeLabel> peLabels = peLabelService.selectList(peLabelWrapper);
        return  new SimpleCoreResponse("200","查询成功",peLabels);
    }
}

